package com.itbaizhan.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itbaizhan.common.CommonResult;
import com.itbaizhan.entity.User;
import com.itbaizhan.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 用户业务层
 */
@DubboService
public class UserServiceImpl implements IUserService{

    @Autowired
    private UserMapper userMapper;

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public CommonResult save(User user) {
        CommonResult commonResult = new CommonResult();
        // 添加用户
        int insert = userMapper.insert(user);
        // 判断是否添加成功
        if (insert > 0){
            // 结果编码
            commonResult.setCode(200);
            // 结果信息
            commonResult.setMessage("success");
        }else {
            // 结果编码
            commonResult.setCode(500);
            // 结果信息
            commonResult.setMessage("服务异常，稍后再试~");
        }
        return commonResult;


    }


    /**
     * 查询用户
     * @param user
     * @return
     */
    @Override
    public CommonResult findByUser(User user) {
        CommonResult commonResult = new CommonResult();
        // 1、条件构造器
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(user.getId() != null,User::getId,user.getId());
        lqw.eq(user.getName() != null,User::getName,user.getName());
        lqw.eq(user.getAge() != null,User::getAge,user.getAge());
        // 2、查询用户
        List<User> users = userMapper.selectList(lqw);
        // 3、结果编码
        commonResult.setCode(200);
        // 4、结果信息
        commonResult.setMessage("success");
        // 5、 结果
        commonResult.setData(users);
        return commonResult;
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @Override
    public CommonResult updateUser(User user) {
        CommonResult commonResult = new CommonResult();
        // 1、条件构造器
        LambdaUpdateWrapper<User> uluw = new LambdaUpdateWrapper<>();
        uluw.set(user.getName() != null ,User::getName,user.getName());
        uluw.set(user.getAge() != null ,User::getAge,user.getAge());
        if (user.getId() == null){
            //结果编号
            commonResult.setCode(500);
            // 结果信息
            commonResult.setMessage("用户id为空~");
            return commonResult;
        }
        uluw.eq(User::getId,user.getId());
        // 更新用户
        int update = userMapper.update(null, uluw);
        if (update > 0){
            //结果编号
            commonResult.setCode(200);
            // 结果信息
            commonResult.setMessage("success");
        }else {
            //结果编号
            commonResult.setCode(500);
            // 结果信息
            commonResult.setMessage("更新失败");
        }

        return commonResult;
    }

    /**
     * 根据用户id去删除用户
     * @param id
     * @return
     */
    @Override
    public CommonResult deleteUser(Long id) {
        CommonResult commonResult = new CommonResult();
        if (id == null){
            // 结果编码
            commonResult.setCode(500);
            // 结果描述
            commonResult.setMessage("用户id为空~");
            return commonResult;
        }
        // 1、根据用户id删除用户
        int i = userMapper.deleteById(id);
        if( i > 0){
            commonResult.setCode(200);
            commonResult.setMessage("success");
        }else {
            commonResult.setCode(500);
            commonResult.setMessage("删除失败~");
        }
        return commonResult;
    }


}
